<?php

namespace App\Jobs;

use App\Exceptions\Arr;
use App\Models\Appoint;
use App\Models\AppointNum;
use App\Models\Family;
use App\Models\FamilyChild;
use App\Models\FamilyPregn;
use App\Models\HospitalImportBigchild;
use App\Models\HospitalImportChild;
use App\Models\HospitalImportPregn;
use App\Models\HospitalUserFamily;
use App\Models\ImportFamily;
use App\Models\Notice;
use App\Models\TempPushRecord;
use App\Wechat\Temp;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class SyncDataBigchild implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $importModel;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(HospitalImportBigchild $importModel)
    {
        $this->importModel=$importModel;
        $this->onQueue('sync_data_bigchild');
        $this->onConnection('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            $importModel = $this->importModel;
            $familyTable = app(Family::class)->getTable();
            $hospitalFamily = app(HospitalUserFamily::class)->getTable();
            $familyChildTable = app(FamilyChild::class)->getTable();
            $familys = Family::query()
                ->select(["{$hospitalFamily}.family_id","{$hospitalFamily}.user_id",])
                ->join($familyChildTable, "{$familyChildTable}.family_id", '=', "{$familyTable}.id")
                ->join($hospitalFamily, "{$hospitalFamily}.family_id", '=', "{$familyTable}.id")
                ->where("{$hospitalFamily}.hospital_id", $importModel->hospital_id)
                ->where(function ($query) use ($familyTable,$importModel,$familyChildTable) {
                    $query->where(function ($query) use ($familyTable,$importModel) {
                        $query->where("{$familyTable}.name", $importModel->field1);
                        $query->where(DB::raw("SUBSTRING({$familyTable}.idcard, -4)"), substr($importModel->field3, -4));
                    });
                    $query->orWhere(function ($query) use ($familyTable,$importModel,$familyChildTable) {
                        $query->where("{$familyChildTable}.mother", $importModel->field7);
                        $query->where("{$familyTable}.name", $importModel->field1);
                    });
                })
                ->distinct()
                ->get();
            if($familys->isNotEmpty()) {
                foreach ($familys as $family) {
                    $row['family_id'] = $family->family_id;
                    $row['user_id'] = $family->user_id;
                    $row['hospital_id'] = $importModel->hospital_id;
                    $row['import_id'] = $importModel->id;
                    $row['type'] = 3;
                    $data[] = $row;
                }
                ImportFamily::query()->upsert($data, ['id']);
            }


        }catch(\Exception $e){


            $this->fail($e->getMessage());
        }
    }
}
